package com.hebada.converter;

import com.hebada.domain.Article;
import com.hebada.entity.ArticleStatus;
import com.hebada.utils.HtmlUtils;
import com.hebada.utils.ImageUtils;
import com.hebada.web.vo.request.ArticleRequest;
import com.hebada.web.vo.request.ArticleSearchRequest;
import com.hebada.web.vo.request.PageRequest;
import com.hebada.web.vo.response.ArticleListResponse;
import com.hebada.web.vo.response.ArticleResponse;
import com.hebada.web.vo.response.PageResponse;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by paddy on 2017/9/9.
 */
@Service
public class ArticleConverter {

  public Article convertToArticle(ArticleRequest request) {
    final Article article = new Article();
    article.setTitle(request.getTitle());
    article.setContent(request.getContent());
    article.setKeyWords(request.getKeyWords());
    article.setCatalogId(request.getCatalogId());
    article.setArticlePageImageUrl(request.getArticlePageImageUrl());
    article.setAuthor("admin");
    article.setStatus(request.getStatus());
    article.setRecommended(request.isRecommended());
    Date now = new Date();
    article.setCreatedTime(now);
    article.setUpdateTime(now);
    if (ArticleStatus.PUBLISHED.equals(request.getStatus())) {
      article.setPublishTimed(now);
    }
    return article;
  }

  public Article convertToArticle(Article article, Article articleDomain) {
    articleDomain.setCatalogId(article.getCatalogId());
    articleDomain.setKeyWords(article.getKeyWords());
    articleDomain.setContent(article.getContent());
    articleDomain.setTitle(article.getTitle());
    articleDomain.setArticlePageImageUrl(article.getArticlePageImageUrl());
    articleDomain.setStatus(article.getStatus());
    //todo: 需要修改为获取发布后的文章内容
    if (ArticleStatus.SAVE.equals(article.getStatus()) && ArticleStatus.PUBLISHED.equals(articleDomain.getStatus())) {
      articleDomain.setPublishTimed(null);
    }
    articleDomain.setRecommended(article.isRecommended());
    articleDomain.setUpdateTime(new Date());
    return articleDomain;
  }

  public ArticleResponse convertToArticleResponse(Article article) {
    if (article == null) return null;
    ArticleResponse response = new ArticleResponse();
    response.setId(article.getId());
    response.setKeyWords(article.getKeyWords());
    response.setTitle(article.getTitle());
    if (!StringUtils.isEmpty(article.getArticlePageImageUrl()))
      response.setArticlePageImageUrl(article.getArticlePageImageUrl());
    else
      response.setArticlePageImageUrl(ImageUtils.getImageUrlFirstFromHtml(article.getContent()));
    response.setContent(article.getContent());
    response.setAuthor(article.getAuthor());
    response.setStatus(article.getStatus());
    response.setRecommended(article.isRecommended());
    response.setCreateTime(formatDate(article.getCreatedTime()));
    response.setPublishTime(formatDate(article.getPublishTimed()));
    return response;
  }

  public Article convertToArticleSearch(PageRequest<ArticleSearchRequest> request) {
    Article articleQuery = new Article();
    if (request.getParams() == null) return articleQuery;
    articleQuery.setTitle(request.getParams().getTitle());
    articleQuery.setCatalogId(request.getParams().getCatalogId());
    articleQuery.setStatus(request.getParams().getStatus());
    return articleQuery;
  }

  public PageResponse<ArticleListResponse> convertToArticlePageResponse(Page<Article> articlePage, int currentPage, int pageSize) {
    PageResponse<ArticleListResponse> pageResponse = new PageResponse<ArticleListResponse>();
    pageResponse.setPageSize(pageSize);
    pageResponse.setCurrentPage(currentPage);
    pageResponse.setTotal(articlePage.getTotalElements());
    pageResponse.setTotalPage(articlePage.getTotalPages());
    if (!articlePage.hasContent()) return pageResponse;
    for (Article article : articlePage.getContent())
      pageResponse.getContent().add(convertToArticleListResponse(article));
    return pageResponse;
  }

  public ArticleListResponse convertToArticleListResponse(Article article) {
    if (article == null) return null;
    ArticleListResponse response = new ArticleListResponse();
    response.setId(article.getId());
    response.setTitle(article.getTitle());
    response.setAuthor(article.getAuthor());
    if (!StringUtils.isEmpty(article.getArticlePageImageUrl()))
      response.setArticlePageImageUrl(article.getArticlePageImageUrl());
    else
      response.setArticlePageImageUrl(ImageUtils.getImageUrlFirstFromHtml(article.getContent()));
    String content = article.getContent();
    if (StringUtils.hasLength(content)) {
      content = HtmlUtils.filterHtmlTag(content);
      response.setDescription(content.length() > 500 ? content.substring(0, 500) : content);
    }
    response.setCreateTime(formatDate(article.getCreatedTime()));
    response.setStatus(article.getStatus().getStatus());
    response.setPublishTime(formatDate(article.getPublishTimed()));
    response.setRecommended(article.isRecommended());
    return response;
  }

  private String formatDate(Date date) {
    if (date == null) return null;
    SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
    return format.format(date);
  }
}
